package org.jeecg.modules.demo.ysd.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.ysd.entity.YsdMedicalMap;
import org.jeecg.modules.demo.ysd.entity.YsdMedicalMapVo;
import org.jeecg.modules.demo.ysd.service.IYsdMedicalMapService;
import org.jeecg.modules.demo.ysd.service.IYsdMedicalMapService2;
import org.jeecg.modules.demo.ysd.tools.AddressConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

@Tag(name = "就医地图")
@RestController
@RequestMapping("/ysd/ysdMedicalMap")
@Slf4j
public class YsdMedicalMapController2 extends JeecgController<YsdMedicalMap, IYsdMedicalMapService> {

    @Autowired
    private IYsdMedicalMapService2 ysdMedicalMapService2;

    @Autowired
    private IYsdMedicalMapService ysdMedicalMapService;


    @Operation(summary = "就医地图-分页列表查询")
    @GetMapping(value = "/list2")
    public Result<IPage<YsdMedicalMapVo>> queryPageList(YsdMedicalMap ysdMedicalMap,
                                                        @RequestParam(name = "lat", required = false) Double lat,
                                                        @RequestParam(name = "lng", required = false) Double lng,
                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                        @RequestParam(name = "name", required = false) String name,
                                                        HttpServletRequest req) {
//        if (Objects.isNull(lat) || Objects.isNull(lng)) {
//            QueryWrapper<YsdMedicalMap> queryWrapper = QueryGenerator.initQueryWrapper(ysdMedicalMap, req.getParameterMap());
//            Page<YsdMedicalMap> page = new Page<>(pageNo, pageSize);
//            IPage<YsdMedicalMap> pageList = ysdMedicalMapService.page(page, queryWrapper);
//            return Result.OK(pageList);
//        } else {

        log.info("lat {} {} ", lat, lng);

        if (Objects.isNull(lat) || Objects.isNull(lng)) {
            lat = 39.908823;
            lng = 116.397470;
        }
        int offset = (pageNo - 1) * pageSize;
        IPage<YsdMedicalMapVo> page = Page.of(pageNo, pageSize);
        List<YsdMedicalMapVo> pageList = ysdMedicalMapService2.selectNearby(lat, lng, offset, pageSize, name);
        pageList.forEach(item -> {
            try {
                String[] dis = item.getDistance().split("\\.");
                item.setDistance(dis[0] + "." + dis[1].substring(0, 2));
            } catch (Exception e) {

            }
        });
        page.setRecords(pageList);
        return Result.OK(page);
//        }

    }


    @GetMapping(value = "/convertAddress")
    public String convertAddress(String address) {
        log.info("转换 " + address);
//      String result = AddressConvert.getGeoCode(address);
//      log.info(JSONObject.toJSONString(result));


        List<YsdMedicalMap> list = ysdMedicalMapService.list();

        list.forEach(item -> {
            if (Objects.isNull(item.getLat())) {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

                if (Objects.isNull(item.getLat())) {
                    String pos = AddressConvert.getGeoCode(item.getName());
                    if (Objects.nonNull(pos)) {
                        String[] value = pos.split(",");
                        if (value.length == 2) {
                            item.setLng(value[0]);
                            item.setLat(value[1]);
                            ysdMedicalMapService.saveOrUpdate(item);
                        }
                    }
                }
            }
        });

        return "ok";

    }


}
